package demo1.web.servlet.userBuyapi;

import demo1.dao.Mapper.userBuyMapper;
import demo1.pojo.userBuy;
import demo1.dao.sql.userBuysql;
import demo1.service.userBuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;

@WebServlet("/checkOrderForm")
public class checkOrderForm extends HttpServlet {//检查有没有存在订单
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        userBuy users = (userBuy) userObject;

        //用session对象的值
//        userBuyMapper userBuyMapper = new userBuysql();
        try {
//            ResultSet rs;
//            rs = userBuyMapper.selectAllBygouwuid(users.getGouwuid());
//            while (rs.next()){
//                if(users.getGouwuid() == rs.getInt("gouwuid")){
//                    //如果存在订单
//
//                    return;
//                }
//            }
            userBuyService check = new userBuyService();
            if(check.checkOrderForm(users.getGouwuid()) == 1){
                //如果存在订单
                resp.sendRedirect("failOrderForm.jsp");
            }
            else {
                //如果没有订单,就可以生成
                resp.sendRedirect("/logindemo2/createOrderForm");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
